package com.zm.controller;

import com.zm.bean.*;
import com.zm.service.CabTestDoorRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = "/cabTestDoorRecord", produces = {"application/json;charset=UTF-8"})
public class CabTestDoorRecordController {

    @Autowired
    private CabTestDoorRecordService cabTestDoorRecordService;

    @RequestMapping(value = "/saveOne", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean saveOne(@RequestBody TestDoorRecordBean mTestDoorRecordBean) {

        if (mTestDoorRecordBean == null) {
            return new ResultBean(CodeEnum.FAIL.getCode(), "测试数据不能为空", null);
        }

        TestDoorRecordBean mBean = cabTestDoorRecordService.save(mTestDoorRecordBean);
        if (mBean == null) {
            return new ResultBean(CodeEnum.FAIL.getCode(), "数据插入失败",null);
        }

        return new ResultBean(CodeEnum.SUCCESS.getCode(), "测试结果上传成功", mBean);
    }


    @RequestMapping(value = "/saveList", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean saveList(@RequestBody List<TestDoorRecordBean> mTestDoorRecordBeans) {

        if (mTestDoorRecordBeans == null || mTestDoorRecordBeans.size() == 0) {
            return new ResultBean(CodeEnum.FAIL.getCode(), "测试数据不能为空", null);
        }

        mTestDoorRecordBeans = cabTestDoorRecordService.saveAll(mTestDoorRecordBeans);
        if (mTestDoorRecordBeans == null || mTestDoorRecordBeans.size() == 0) {
            return new ResultBean(CodeEnum.FAIL.getCode(), "数据插入失败", null);
        }

        return new ResultBean(CodeEnum.SUCCESS.getCode(), "测试结果上传成功", null);
    }


    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public String showTestDoorRecord(ModelMap modelMap,
                                     @RequestParam(required = false) String cid,
                                     @RequestParam(required = false, defaultValue = "1") int curPage,
                                     @RequestParam(required = false, defaultValue = "100") int pageCount,
                                     @RequestParam(required = false, defaultValue = "3000") int pageSize
    ) {

        List<TestDoorRecordBean> testDoorRecordBeans;
        int totalElements = 0; //总条数
        int totalPages = 0; // 总页数
        Page<TestDoorRecordBean> pageRecords;

        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(curPage, pageSize, sort);

        if (cid == null) {
            modelMap.addAttribute("cabinetId", "全部");
            pageRecords = cabTestDoorRecordService.findAll(pageable);

            totalElements = (int) pageRecords.getTotalElements(); //总条数
            totalPages = pageRecords.getTotalPages(); // 总页数
            testDoorRecordBeans = pageRecords.getContent(); // 数据列表

        } else {
            modelMap.addAttribute("cabinetId", cid);
            pageRecords = cabTestDoorRecordService.findAllByCabinetId(cid, pageable);
            totalElements = (int) pageRecords.getTotalElements(); //总条数
            totalPages = pageRecords.getTotalPages(); // 总页数
            testDoorRecordBeans = pageRecords.getContent(); // 数据列表
        }


        modelMap.addAttribute("pageSize", pageSize);
        modelMap.addAttribute("curPage", curPage);
        modelMap.addAttribute("pageCount", totalPages);
        modelMap.addAttribute("totalCount", totalElements);

        modelMap.addAttribute("recordBeanList", testDoorRecordBeans);
        return "testDoorRecord";
    }
}
